package com.lagou.edu.service.impl;

import com.lagou.edu.dao.UserAuthCodeDao;
import com.lagou.edu.dao.UserDao;
import com.lagou.edu.dao.UserTokenDao;
import com.lagou.edu.entity.User;
import com.lagou.edu.entity.UserAuthCode;
import com.lagou.edu.entity.UserToken;
import com.lagou.edu.service.UserService;
import com.lagou.edu.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * @date 2020/5/26 19:57
 */
@Slf4j
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserTokenDao userTokenDao;

    @Autowired
    UserAuthCodeDao userAuthCodeDao;

    @Autowired
    UserDao userDao;

    @Override
    public Result login(String email, String password, HttpServletResponse response) {
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)){
            return Result.error("参数错误");
        }

        List<User> userList = userDao.findUserByEmailAndPassword(email, password);
        if (userList == null){
            List<User> userByEmail = userDao.findUserByEmail(email);
            if (CollectionUtils.isEmpty(userByEmail)){
                return Result.error("用户不存在，请注册");
            }
            return Result.error("账户密码错误");
        }
        List<UserToken> userTokenList = userTokenDao.findUserTokenByEmail(email);
        if (CollectionUtils.isEmpty(userTokenList)){
            return Result.error("用户不存在");
        }
        Cookie cookie = new Cookie("lagouSessionId", userTokenList.get(0).getToken());
        cookie.setPath("/");
        response.addCookie(cookie);
        return Result.ok();
    }

    @Override
    public Result register(String email, String password, String code, HttpServletResponse httpServletResponse) {
        /**
         * 参数校验
         */
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(code) || StringUtils.isEmpty(password)){
            return Result.error("参数错误");
        }

        //1.查看该用户是否注册过
        List<User> userByEmailList = userDao.findUserByEmail(email);
        if (!CollectionUtils.isEmpty(userByEmailList)){
            return Result.error("用户已存在，请登录");
        }

        //2.判断验证码是否正确
        List<UserAuthCode> userAuthCodeList = userAuthCodeDao.findUserAuthCodeByEmailOrderByExpiretimeDesc(email);
        if (CollectionUtils.isEmpty(userAuthCodeList)){
            return Result.error("请重新获取验证码");
        }else{
            UserAuthCode userAuthCode = userAuthCodeList.get(0);
            if (!userAuthCode.getCode().equals(code)){
                return Result.error("验证码输入错误，请重新输入");
            }
        }

        //3.注册信息存入数据库
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        userDao.save(user);

        //4.生成Token
        UUID token = UUID.randomUUID();

        //5.存入数据库
        UserToken userToken = new UserToken();
        userToken.setEmail(email);
        userToken.setToken(token.toString());
        userTokenDao.save(userToken);

        //6.带入cookie返回
        Cookie cookie = new Cookie("lagouSessionId", token.toString());
        cookie.setPath("/");
        httpServletResponse.addCookie(cookie);
        return Result.ok("注册成功");
    }

    @Override
    public Result queryUserInfoByToken(String token) {
        List<UserToken> userTokenList = userTokenDao.findUserTokenByToken(token);
        if (CollectionUtils.isEmpty(userTokenList)){
//            return Result.error();
        }
//        userTokenList.get(0).getEmail()
        return Result.ok();
    }

    @Override
    public Boolean isRegistered(String email) {
        List<User> userList = userDao.findUserByEmail(email);
        if (CollectionUtils.isEmpty(userList)){
            return true;
        }
        return false;
    }


}
